<?php
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

class Github extends BaseOauth{

	protected $GetRequestCodeURL = 'https://github.com/login/oauth/authorize';
	protected $GetAccessTokenURL = 'https://github.com/login/oauth/access_token';
	protected $ApiBase = 'https://api.github.com/';

	public function call($api, $param = '', $method = 'GET', $multi = false){
		$params = array();
		$header = array("Authorization: bearer {$this->Token['access_token']}");
		$data = $this->http($this->url($api), $this->param($params, $param), $method, $header);
		return json_decode($data, true);
	}

	protected function parseToken($result, $extend){
		parse_str($result, $data);
		if($data['access_token'] && $data['token_type']){
			$this->Token = $data;
			$data['openid'] = $this->openid();
			return $data;
		} else
			throw new Exception("获取 Github ACCESS_TOKEN出错：未知错误");
	}

	public function openid(){
		if(isset($this->Token['openid']))
			return $this->Token['openid'];

		$data = $this->call('user');
		if(!empty($data['id']))
			return $data['id'];
		else
			throw new Exception('没有获取到 Github 用户ID！');
	}

	public function getuserinfo($token){
		$data  = $this->call('user');

		if(empty($data['code'])){
			$userInfo['type'] = 'GITHUB';
			$userInfo['name'] = $data['login'];
			$userInfo['nick'] = $data['name'];
			$userInfo['head'] = $data['avatar_url'];
			return $userInfo;
		} else {
			throw_exception("获取Github用户信息失败：{$data}");
		}
	}

}
